<template>
	<view class="home-wrap u-m-b-20">
		<base-nav-bar :backIconColor="'#333'" :isBack="true" :background="{'background':'#fff'}">
			<text
				slot="content"
				style="color: #333; width: 100%; text-align: center;padding-left: 20rpx;"
			>
				新增课程包
			</text>
		</base-nav-bar>
		<view class="content-box u-p-20 u-m-t-30 u-m-l-40 u-m-r-40">
			<!-- grid商家功能模块 -->
			<view class="u-flex u-flex-items-center u-flex-start u-flex-wrap">
				<u--form :model="form" ref="uForm" :labelPosition="'top'" style="width: 100%;" :labelStyle="{'margin-botttom':'10rpx'}">
					<u-form-item label="课程包名称" prop="name" labelWidth="120">
						<u-input v-model="form.name" placeholder="请输入课程名称"/>
					</u-form-item>
					<u-form-item label="课程包介绍" prop="description" labelWidth="120">
						<u--textarea v-model="form.description" placeholder="请输入课程介绍" ></u--textarea>
					</u-form-item>
					<u-form-item label="课程包价格(元)" prop="price" labelWidth="120">
						<u-input v-model="form.price" placeholder="请输入课程价格"/>
					</u-form-item>
					<u-form-item label="请上传课程包图片" prop="name" labelWidth="130">
						<u-upload
							:fileList="courseImages"
							@afterRead="afterRead"
							@delete="deletePic"
							name="Images"
							multiple
							:previewFullImage="true"
						></u-upload>
					</u-form-item>
				</u--form>
				<view class="submit-btn" @click="submit">提交</view>
			</view>
		</view>
	</view>
</template>

<script>

import { mapGetters } from 'vuex';
import { HTTP_IMG_URL } from '../../config/app.js'
import BaseNavBar from "../../components/BaseNavBar.vue";

export default {
	components: {
		BaseNavBar
	},
	data() {
		return {
			showTeacher:false,
			showClass:false,
			courseImages:[],
			form: {
				name: '',
				description:'',
				price:'',
				image:'',
			},
			courseId:'',
			typeList:[],
			tagsList:[],
			teacherList:[],
			rules: {
				name: [
					{
						required: true,
						message: '请输入课程包名称',
						trigger: ['blur', 'change']
					}
				]
			},
		};
	},
	computed: {
		...mapGetters(['isLogin','userInfo']),
		
	},
	onPullDownRefresh() {
	
	},
	
	onShow() {
	
	},
	onHide() {
	},
	onLoad(options) {
		console.log("====options",options)
		if(options&&options.id){
			this.courseId = options.id
			setTimeout(()=>{
				this.getCourseDetails()
			},500)
		}
	},
	onReady() {
		//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		this.$refs.uForm.setRules(this.rules)
	},
	methods: {
		valChange(e){
			this.form.hours = e
		},
		HTTP_IMG_URL() {
			return HTTP_IMG_URL
		},
		// 路由跳转
		jump(path, parmas) {
			uni.navigateTo({
				url: path,
				query: parmas
			});
		},
		submit() {
			this.form.course_label_id_arr = [];
			let imageArr = [];
			if(this.courseImages.length>0){
				this.courseImages.forEach(item=>{
					imageArr.push(item.url)
				})
			}
			this.$refs.uForm.validate().then(res => {
				let fromData = {
					...this.form,
					images:imageArr.join(','),
					image:imageArr.join(','),
				}
				if(this.courseId){
					fromData = {
						...fromData,
						id:this.courseId
					}
					this.$Api.merchant.wrapEditPost(fromData).then(res=>{
						uni.$u.toast('修改成功')
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},500)
					}).catch(err=>{
						console.log(e)
					})
				}else{
					this.$Api.merchant.wrapAdd(fromData).then(res=>{
						uni.$u.toast('新增成功')
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},500)
					}).catch(err=>{
						console.log(e)
					})
				}
			}).catch(errors => {
				uni.$u.toast('校验失败')
			})
		},
		// 删除图片
		deletePic(event) {
			this[`course${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`course${event.name}`].length
			for (let i = 0; i < lists.length; i++) {
				const result = await this.$uploadFilePromise(lists[i].url)
				this[`course${event.name}`].splice(fileListLen, 1, {
					status: 'success',
					message: '',
					url: result.fullurl
				})
				fileListLen++
			}
			console.log("===this.tagsList",this.tagsList);
		},
		getCourseDetails(){
			let that = this;
			that.$Api.merchant.wrapDetail({id:this.courseId}).then(res=>{
				this.form = res.data;
				this.courseImages = [];
				let imageArr = that.form.image.split(',');
				imageArr.forEach(item=>{
					let obj = {
						url: item
					}
					that.courseImages.push(obj)
				})
			}).catch(err=>{
				console.log(e)
			})
		}
	}
};
</script>

<style lang="scss" scoped>
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
.home-wrap {
	min-height: 100vh;
	background: #F6F6F8;
	position: relative;
}
.kehu-item {
	border-radius: 20rpx;
	width: 100%;
	.swipers{
		width: 100%;
	}
}
.nickname{
	font-size: 34rpx;
	font-weight: bold;
}
.content-box{
	background: #FFFFFF;
	border-radius: 30rpx;
	min-height: 80vh;
	.pay-item{
		width: 25%;
	}
}
::v-deep .u-form{
	width: 100%;
}
.images-content{
	width: 100%;
	height: 300rpx;
	display: flex;
	border:1rpx solid #eee;
}
.submit-btn{
	margin-top: 20rpx;
	width: 100%;
	height: 84rpx;
	background: linear-gradient( #30AA4D 0%, #20CE83 100%);
	box-shadow: 0px 2px 4px 0px rgba(48,170,77,0.1);
	border-radius: 8px;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #fff;
	font-size: 28rpx;
}
.u-page__tag-item {
	margin-right: 20px;
	margin-top: 20rpx;
	&:nth-of-type(1) {
		margin-top: 0rpx;
	}
	&:nth-of-type(2) {
		margin-top: 0rpx;
	}
	&:nth-of-type(3) {
		margin-top: 0rpx;
	}
}
::v-deep .u-tag--success--plain{
	background: #F6F6F8 !important;
	border-color: #EDEDED !important;
	color: #C7C7C7 !important;
}
::v-deep .u-tag__text--success--plain{
	color: #C7C7C7 !important;
}
::v-deep .u-tag--success{
	background: linear-gradient(#30AA4D 0%, #20CE83 100%);
	box-shadow: 0px 2px 4px 0px rgba(48,170,77,0.1);
	border-width: 1px;
	border-color: #30AA4D;
}
.teShu-item{
	::v-deep .u-form-item__body{
		flex-direction: row!important;
	}
}
</style>
